package com.etc.controller;

import java.util.Random;

import com.etc.entity.Role;
import com.etc.service.RoleService;

public class DamageProcess {

	public void startDual(Role role1, Role role2) {
		int damageA = 0;
		int damageB = 0;
		int i;
		int j;

		System.out.println(role1.getNickname() + "血量为:" + role1.getHeal());
		System.out.println(role2.getNickname() + "血量为:" + role2.getHeal());
		while (true) {
			Random ran = new Random();
			i = ran.nextInt(role1.getCareer().getSkills().size());
			j = ran.nextInt(role2.getCareer().getSkills().size());
			role1.getCareer().getSkills().get(i).useSkill(role1, role2);
			role2.getCareer().getSkills().get(i).useSkill(role2, role1);

			damageA = role2.getAttack() - role1.getDefense();
			damageB = role1.getAttack() - role2.getDefense();

			role1.reduceHeal(damageA);
			role2.reduceHeal(damageB);

			System.out.println(role2.getNickname() + "对" + role1.getNickname() + "造成了" + damageA + "点伤害,"
					+ role1.getNickname() + "的血量剩下" + role1.getHeal());
			System.out.println(role1.getNickname() + "对" + role2.getNickname() + "造成了" + damageB + "点伤害,"
					+ role2.getNickname() + "的血量剩下" + role2.getHeal());
			if (role1.getHeal() == 0) {
				System.out.println(role2.getNickname() + "胜利了");
				break;
			}
			if (role2.getHeal() == 0) {
				System.out.println(role1.getNickname() + "胜利了");
				break;
			}
			endRound(role1, role2);
		}

	}

	public String endRound(Role role1, Role role2) {
		String endMsg1 = role1.getCareer().endRound(role1, role2);
		String endMsg2 = role2.getCareer().endRound(role2, role1);
		return endMsg1+ "#" + endMsg2;
	}

	public String startDualEnemy(Role role1, Role role2) {
		int damageA = 0;
		int damageB = 0;
		

		damageA = role2.getAttack() - role1.getDefense();
		damageB = role1.getAttack() - role2.getDefense();

		role1.reduceHeal(damageA);
		role2.reduceHeal(damageB);

		String damageMsg1 = role2.getNickname() + "对" + role1.getNickname() + "造成了" + damageA + "点伤害,"
				+ role1.getNickname() + "的血量剩下" + role1.getHeal();
		String damageMsg2 = role1.getNickname() + "对" + role2.getNickname() + "造成了" + damageB + "点伤害,"
				+ role2.getNickname() + "的血量剩下" + role2.getHeal();
		
		if (role1.getHeal() == 0) {
			damageMsg1 +="," + role2.getNickname() + "胜利了";
			damageMsg2 = "";
		}
		if (role2.getHeal() == 0) {
			damageMsg2 += "," + role1.getNickname() + "胜利了";
			damageMsg1 = "";
		}
		return damageMsg1 + "#" + damageMsg2;
	}

	public String useSkillMsg(Role role1, Role role2, int skillIndex1, int skillIndex2) {
		String useSkill1 = role1.getCareer().getSkills().get(skillIndex1).useSkill(role1, role2);
		String useSkill2 = role2.getCareer().getSkills().get(skillIndex2).useSkill(role2, role1);
		return useSkill1+"#"+useSkill2;
	}
	
	public String useSkillMsgOnce(Role role1, Role role2, int skillIndex1) {
		String useSkill1 = role1.getCareer().getSkills().get(skillIndex1).useSkill(role1, role2);
		return useSkill1;
	}

	public String startDualOnce(Role role, Role enemy) {
		
		int damageB = role.getAttack() - enemy.getDefense();

		enemy.reduceHeal(damageB);

		String damageMsg2 = role.getNickname() + "对" + enemy.getNickname() + "造成了" + damageB + "点伤害,"
				+ enemy.getNickname() + "的血量剩下" + enemy.getHeal();
		
		if (enemy.getHeal() == 0) {
			damageMsg2 += "," + role.getNickname() + "胜利了";
		}
		return damageMsg2;
	}
	
	public static void main(String[] args) {
		DamageProcess dp = new DamageProcess();
		RoleService roleSer = new RoleService();
		Role role1 = roleSer.findRoleById("397ccde9");
		Role role2 = roleSer.findRoleById("859b46cc");
		dp.startDual(role1, role2);
	}
}
